Custom Taxonomy Pulls


I created a custom taxanomy pull for a previous wordpress theme, and I was technical enough to know why it was working before, and sort of understand why its not now, and at the same time not entirely sure how to fix it.

I created a page type called Towns and created single-towns.php (real estate related website) which it automatically loads for those pages.

This is the code I used before to call the information out of the database.

// get town information 
// get town name 
$town = get_field('town_name');
$town = get_term($town, 'town_name');
$town = $town->name;

$intro = get_field('town_intro');
$image = get_field('showcase_image');

$schools = get_field('schools');
$commute = get_field('commute');
$recreation = get_field('recreation');
$shopping = get_field('shopping');
$restaurants = get_field('restaurants');
$links = get_field('links');
$search = get_field('property_searches'); 

Then when I wanted to call the custom field I would insert the following code into the HTML structure where needed.

<?php echo $schools; ?> 

It breaks down when inserting that into:

// Replace the main loop with my own section.
beans_modify_action_callback( 'beans_loop_template', 'town_content' );

function town_content() {

    ?>

    <!-- Structured HTMLS -->

I understand its because it is attempting to open a php tag inside a php tag, just dont know how to fix it using beans.

I am using advanced custom fields and and custom page type/taxanomy plugin to create the fields (would love to convert it to beans, however, I dont fully understand that at the moment), so for now I just am looking to call the custom fields I already have in the system into the HTML .


You're not opening a PHP tag in a PHP tag... You closed it in your function. Can you please indicate the error when you say "it broke down now"?

Can you paste the full code for town_content function?

Thanks!


I was not getting an error code, just a white screen.

Now I have gotten to the point where I am outputting the page, however, it isn't grabbing any of the custom inputs.

It isnt outputting the image for the header - its grabbing the page url instead of the image url and its stored as the image url (code works, I tested it with a static image url) and it isnt writting any of the layout code.

<?php
/**
 * The Template for displaying town pages
 * Template Name: Town Pages

 */

// get page fields

add_action( 'beans_uikit_enqueue_scripts', 'my_function_name' );

function my_function_name() {

 beans_uikit_enqueue_components( true );
 beans_uikit_enqueue_components( true, 'add-ons' );

}

// get town information 
// get town name 
$town = get_field('town_name');
$town = get_term($town, 'town_name');
$town = $town->name;

$intro = get_field('town_intro');

$image = get_field('showcase_image');

$schools = get_field('schools');
$commute = get_field('commute');
$recreation = get_field('recreation');
$shopping = get_field('shopping');
$restaurants = get_field('restaurants');
$links = get_field('links');
$search = get_field('property_searches'); 

// Add hero after header.
add_action( 'beans_header_after_markup', 'parallax_hero' );

function parallax_hero() {

?>
 <div class="tm-hero uk-block-large">
    <div style="width: 100%; height: 50vh; background-image: url('<?php echo $image; ?>')" data-uk-parallax="{bg: '-200'}">
   </div>
  </div>  

<?php
}

// Replace the main loop with my own section.
beans_modify_action_callback( 'beans_loop_template', 'town_content' );

function town_content() {

    ?>

  <div class=".uk-width-4-5">
   <h2>Welcome to <?php echo $town; ?>!</h2>

<?php if (!empty($intro)) {   

      echo $intro;

  } else { echo "";} 

    if (!empty($schools)) 
      {
     ?>  
        <h2><i class="fa fa-graduation-cap"></i><?php echo $town; ?> Schools </h2>

        <?php echo $schools; ?>

    <?php
     } else { echo "";} 
     if (!empty($commute)) {
     ?>    
    <h2><i class="fa fa-train"></i>  <?php echo $town; ?> Commuters</h2>

    <?php echo $commute; ?>

    <?php
     } else { echo "";} ?>

      <h2><i class="fa fa-line-chart"></i>  The <?php echo $town; ?> Market</h2>

<!-- Insert Latest Market Report About Town -->   
<?php global $post;
$args = array( 'numberposts' => 1, 'category_name' => 'market-update', 
'tax_query' => array(
        array(
            'taxonomy' => 'town_name',
            'field' => 'slug',
            'terms' => $town,
        )
    ),

 );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); 

?>

<div><?php the_content(); ?></div>

<?php endforeach; 

// End of Market Report

   if (!empty($recreation)) {
     ?>
        <h2><i class="fa fa-ticket"></i>  <?php echo $town; ?> Recreation</h2>

    <?php echo $recreation; ?>

    <?php
     } else { echo "";} ?>

    <?php if (!empty($shopping)) { 
     ?>  
      <h2><i class="fa fa-shopping-cart"></i>  Shop <?php echo $town; ?></h2>

      <?php echo $shopping; ?>

    <?php 
    } else { echo "";} ?>

  <?php if (!empty($restaurants)) {
?> 
      <h2><i class="fa fa-cutlery"></i>  <?php echo $town; ?> Eats</h2>

      <?php echo $restaurants; ?>

    <?php
   } else { echo "";} ?>

 </div>
<div class=".uk-width-1-5"> 

      <h2><i class="fa fa-search"></i> <?php echo $town; ?>  Home Search</h2>

        <?php echo $search; ?>

      <h2><i class="fa fa-link"></i> <?php echo $town; ?>  Links</h2>

    <?php
    // Get the ID of a given category
    $category_id = get_cat_ID($town);

    // Get the URL of this category
    $category_link = get_category_link( $category_id ); ?>

<!-- Print a link to this category -->
<a href="<?php echo esc_url( $category_link ); ?>" title="<?php echo $town ?>">Our Blog Posts on <?php echo $town ?></a>

    </br>
   <?php echo $links; ?>

</div> 

<?php social_warfare(); 

}

// Load the document which is always needed at the bottom of template files.
beans_load_document();

If you just echo the contents of $image what do you get? I also noticed that you used this line to try and output your own template content:

beans_modify_action_callback( 'beans_loop_template', 'town_content' );

Try use this instead:

beans_modify_action( 'beans_content_template', 'beans_main_append_markup', 'town_content' );

Let me know how that goes.


I can't see the resulting markup of your code, however I suspect that this line:

add_action( 'beans_header_after_markup', 'parallax_hero' );

Is causing an issue as you probbaly don't even have the beans header being displayed in your template. Can you confirm that it is there by inspecting the markup?

Simon


I made your suggested change to which function to call, and it doesnt seem to make a difference.

The entire HTML structure is propertly written except the content being pulled from the fields or content that wont exist if there is a null value for a variable. Those are all missing. Header tags are there, with navigation. Hero exists, without the image (it defines the (background-image: url() as the page URL. Everything in .uk-width-4-5 and .uk-width-1-5 that isn't depending on a defined variable, pulls just fine. Anything that looks for a defined variable in the if else statements doesnt output, which makes me believe its pulling a null value or beans just handles variables like this differently. Even if I define the variable, it doesnt seem to work. See below for the changes I made:

// get town information 
// get town name 
$town = get_field('town_name');
$town = get_term($town, 'town_name');
$town = $town->name;
$town = 'Fake Town Name';

$intro = get_field('town_intro');

$image = get_field('showcase_image');
$image = 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Bobcat-Texas-9110.jpg/220px-Bobcat-Texas-9110.jpg';

I tested it by writing in variable for both $town and $image, and it still doesnt output.


Ok, I think I solved it, the variables need to be called in the function itself. I have them outside the function.

Which leads me to my next question, is there a way to call them once in a seperate function to be used on the entire page?


If you have to use them in a separate function (and I do not see why you do) then you can always define them as globals.

Write a reply

Login or register to write a reply, it's free!