• Hi Everyone,
    I have a problem displaying the excerpt of a custom post type that is driving me nuts!
    I have registered the excerpt in the custom post using supports as follows: –
    'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'revisions', 'page-attributes' ),
    This shows the excerpt in the admin interface.
    The trouble is when I use get_the_excerpt() I don’t seem to get anything. I know it is there, because it’s in the admin area and others things are showing it, but when I write specific code to include it, nothing, in fact it breaks the map I am supposed to be showing it in. At the moment my code for the map bubble is: –

    <?php
    $title = get_the_title();
    $excerpt = get_the_excerpt();
    $content = get_the_content();
    $permalink = get_the_permalink();
    ?>
    ['<?php echo("<div style=\"width:400px;height:160px;\">".the_post_thumbnail( 'thumbnail', array( 'class' => 'map-bubble-align' ) )."<div><a href=\"".$permalink."\" ><h2>".get_the_title()."</h2></a><p>".$excerpt." </p><a href=\"".$permalink."\"<p>Read More...</p></a></div></div>"); ?>', <?php echo $location['lat']; ?>, <?php echo $location['lng'];?>, <?php $NUM++ ?>],

    The thumbnail, permalink and title are all fine, but the excerpt and content don’t load and usually stop the map from loading. I have also tried using get_the_excerpt() directly rather than via a variable and that doesn’t work either.
    The page on the site where I have this is currently https://www.brenclosures.com.au/projects-locations.htm btw if you want to have a look.
    Any ideas anyone? I am totally at a loss. I have tried everything I can think of but am getting nowhere!
    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Take a look at the WordPress Codex: Querying by Post Type and Custom Post Types in the Main Query

    If you are querying posts in the template:
    Instead of get_the_excerpt() or get_the_content(), use the_excerpt() or the_content()

    Thread Starter funkygorilla

    (@funkygorilla)

    Hi NeoTechnomad

    Thanks for the reply. This has been doing my head in.

    I’ve tried the_excerpt and dropping the get_ on all the others too, both with now luck. I had a look at the links too and everything seems to be in place there too. The weird bit as you can see is that the $title works without any issues it’s content and excerpt I have problems with. If I add those, the whole thing breaks.

    It’s very weird because I know they are there. If you look at https://www.brenclosures.com.au/projects-applications.htm you can see them there. This uses a VC post grid, but they are the excerpts, they just don’t work in the map!

    I’ve tried moving positioning around, using variables, not using variables, using get_, not using _get etc etc and always the same thing, titles, permalink and location info in the custom post work fine, the excerpt and the post don’t…baffling!

    Anyway, thanks for the help, if you can think of anything else let me know.

    Cheers
    Simon

    Where did you get the code from, or did you write it yourself?

    Thread Starter funkygorilla

    (@funkygorilla)

    Sorry about the delay in getting back on to this. I thought I had it sorted over the weekend, but no!

    I got the code from https://www.ads-software.com/support/topic/advanced-custom-fields-display-google-map-on-your-theme and I’ve made a few changes (the original post from this was from Advanced Custom Fields on https://thestizmedia.com/shortcode-to-show-map-markers-of-child-pages-with-acf-pro/).

    As mentioned, the thing I am really struggling with is why I can get the titles, link, map location, featured image but not the excerpt or post content. As soon as I add those, the whole map disappears from the page.

    Currently the map is working for everything but the excerpt using the following code:

    <?php
      $args = array(
    	  'post_type'		=> 'project',
    	  'posts_per_page'	=> -1
      );
    // query
    $wp_query = new WP_Query( $args );
    $NUM = 0;
    ?>
    
      <div id="map" style="width: 100%; height: 700px;"></div>
    
    <script type="text/javascript">
        var locations = [<?php while( $wp_query->have_posts() ){
    	$wp_query->the_post();
        $location = get_field('project_location'); // IMPORTANT << Change this for your Google map field name !!!!!!
    ?>
    
    <?php
    $title = get_the_title();
    $excerpt = get_the_excerpt();
    $permalink = get_the_permalink();
    ?>
    
    <!-- ADD CODE HERE TO IMPORT OTHER INFO -->
    ['<?php echo("<div style=\"width:400px;height:160px;\">".the_post_thumbnail( 'thumbnail', array( 'class' => 'map-bubble-align' ) )."<div><a href=\"".$permalink."\" ><h2>".get_the_title()."</h2></a><p>".$title." content</p><a href=\"".$permalink."\"<p>Read More...</p></a></div></div>"); ?>', <?php echo $location['lat']; ?>, <?php echo $location['lng'];?>, <?php $NUM++ ?>],
    
       <?php } ?> ];
    <!-- END ADD CODE HERE TO IMPORT OTHER INFO -->
    
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4, /*Here you change zoom for your map*/
          center: new google.maps.LatLng(-20.308, 140.1245), /*Here you change center map first location*/
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    
        var infowindow = new google.maps.InfoWindow();
    
        var marker, i;
    
    <!-- To customise the icon, upload your icon to WordPress as a normal image, copy the url and paste it into the location below (icon: 'link goes here') -->
        for (i = 0; i < locations.length; i++) {
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map,
    		icon: 'https://www.brenclosures.com.au/wp-content/uploads/map-icon-2.png' // null = default icon
          });
    
          google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
            }
          })(marker, i));
        }
     </script>

    As soon as I add the excerpt either using a variable or adding the_excerpt() or get_the_excerpt(), the map breaks and I just get an empty div where the map should go!

    btw – Thanks again for helping me out here. I am a php noob I must admit, but I can’t see anything here that might be causing the problem.

    So to clarify…

    I have a problem displaying the excerpt of a custom post type

    This is a page, not a post, correct? yes/no
    You want to display this on your front page? yes/no
    (…if no, then where?)

    Can you put all the text and the map in question together and post a link to that.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display excerpt in Custom Post Type’ is closed to new replies.