AMP Custom GA Dimensions Not Working in GTM - google-analytics

I am running a WordPress website with both traditional and AMP pages. Both versions are using GTM, but the AMP version is not able to find my custom variables. The AMP pages are validating, so I think there is an issue with how I am defining my custom variables
I am using the following code to define the variables on AMP pages. Is there anything out of place that I am missing. to see an example of an AMP page visit https://cellculturedish.com/evolution-stem-cell-culture-cell-derived-extracellular-matrices/amp/.
<body>
<!-- Google Tag Manager -->
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=GTM-xxxxxxxxx.url=SOURCE_URL" data-credentials="include">
<?php if ( is_singular() ) { ?>
<?php global $post; ?>
<?php $categories = get_the_category( $post->ID ); ?>
<?php $primary_category = $categories[0]; ?>
<?php $post_sponsorship = get_field('post_sponsorship', $post->ID); ?>
<?php $is_sponsored = $post_sponsorship['is_sponsored']; ?>
<?php $author = 'Multiple Authors'; ?>
<?php $author_options = get_field('author_options', $post->ID); ?>
<?php if ( $author_options == 'ccd' && get_field('ccd_author') ) { ?>
<?php $author_obj = get_field('ccd_author', $post->ID); ?>
<?php $author = get_userdata( $author_obj ); ?>
<?php $author = $author->data->display_name; ?>
<?php } elseif ( $author_options == 'guest_post' && count( get_field('contributing_experts', $post->ID) ) == 1 ) { ?>
<?php $author_array = get_field('contributing_experts', $post->ID); ?>
<?php $author = get_expert_title( $author_array[0] ); ?>
<?php } ?>
<script type="application/json">
{
"vars": {
"publishDate": "<?php echo get_the_date('', $post->ID); ?>",
"postCategory":"<?php echo $primary_category->name; ?>",
"isSponsored": "<?php echo $sponsored = $post_sponsorship['is_sponsored'] == true ? 'true' : 'false'; ?>",
<?php if ( $post_sponsorship['is_sponsored'] == true && $post_sponsorship['sponsor'] != null ) { ?>
"postSponsor": "<?php echo get_the_title( $post_sponsorship['sponsor'] ); ?>",
<?php } ?>
"postAuthor": "<?php echo $author; ?>"
}
}
</script>
<?php } ?>

Create AMP Variables in GTM for all the custom dimensions you have under "vars" like so:
Next go to your GA Tag, under "more settings" > "custom dimensions" add in the correct index number for your custom dimensions and the amp variable name you just created for them:

Related

Get custom fields without _edit_last, _edit_lock, _wp_page_template and _visual-subtitle

I'm getting custom fields using
<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
<?php echo $key.': '.$value[0].'<br />'; ?>
<?php } ?>
and it is showing
_edit_last: 1<br>
_edit_lock: 1483226440:1<br>
_wp_page_template: page-services.php<br>
Body Repair: ValueBodyRepair<br>
_visual-subtitle: <br>
I need only 4th row Body Repair: ValueBodyRepair<br>
If you would adjust the code this way, you would get only the one you need:
<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
<?php if(substr($key, 0, 1) !== '_'): ?>
<?php echo $key.': '.$value[0].'<br />'; ?>
<?php endif; ?>
<?php } ?>

How to create wordpress shortcode by this code

Sir,
I have this code to show all post of category and thumbnail for 1st post of them.
<?php $recent = new WP_Query(); ?>
<?php $recent->query( 'cat=1&showposts=5' ); ?>
<?php $is_first_post = true; ?>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul>
<li>
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail();
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile; ?>
But I want show this using shortcode. which using category & post number
but I can not make shortcode. Please help me.
// Add Shortcode
function recentpost_shortcode_func() {
$recent = new WP_Query();
$recent->query( 'cat=1&showposts=5' );
$is_first_post = true;
$html = '';
while( $recent->have_posts() ) : $recent->the_post();
$html .='<ul><li>';
if ( $is_first_post && has_post_thumbnail() ) {
$html .=get_the_post_thumbnail();
$is_first_post = false;
}
$html .='<a href="'.get_the_permalink().'">';
$html .=get_the_title();
$html .='</a></li></ul>';
endwhile;
return $html;
}
add_shortcode( 'recentpost', 'recentpost_shortcode_func' );

wp_get_attachment_url not working

I have a template file which shows the posts of a category called downloads. for each posts i have attached a pdf file. I have given a link to download the pdf file on the page. But when i click the download link it goes to the post page and from there i have to click to download the file. Is there any way to directly download without going to the post. ? I have tried using wp_get_attachment_url as the hyper-reference.but it is not working.The code that i have used is below:
<?php /*
Template Name: Downloads Template
*/
?>
<?php get_header(); ?>
<?php
$recent = new WP_Query("cat=7&orderby=title&order=ASC");
while($recent->have_posts()):$recent->the_post();
$desc_values = get_post_custom_values("description");
?>
<div id="download_featured_image" class="<?php the_ID(); ?> download_image_title_desc">
<a href="<?php the_permalink() ?>" rel="title">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<a href = "" > <?php if ( is_user_logged_in() ) {
echo "Download";
}?></a>
<a href=" http://localhost/wordpress/login.php"> <?php if( !(is_user_logged_in()) )
{
echo "Please signup/login to download this file";
}
?>
</a>
<div id="Download_post_description">
<?php
if( is_array( $desc_values ) )
{
foreach($desc_values as $key => $value );
echo "$value</n>";
}
?>
</div>
</div>
<?php endwhile ?>
<?php get_footer(); ?>
I want to give the link to the uploaded pdf in the href which i have left blank. Can someone help me?
Add the PDF attachment ID as a custom field value, for example attached_pdf_id.
Get the URL using wp_get_attachment_url()
-
<?php
if ( is_user_logged_in() ) {
$pdf_link = wp_get_attachment_url( get_post_meta( get_the_ID(), 'attached_pdf_id', true ) );
if ( $pdf_link ) {
?><a href = "<?php echo $pdf_link ?>" >Download</a><?php
} else {
?>Sorry, no link available. Please contact the webmaser.<?php
}
}
?>

How to list only subpages title and link in a wordpress page div

I have a webblog using wordpress i'm using wp_list_pages() function to get list its Sub Pages Links (excluding main page) in a list, I've tried the below codes but its returning all Pages Sub Page.
I also tried this Argument child_of but its not happning. Please suggest i have tried so many things...
<?php
$output = wp_list_pages('echo=0&depth=1&child_of=0');
if (is_page( )) {
$page = $post->ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
}
echo $output;
?>
This code will list the subpages and siblings of the current page:
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
}
else {
$parent = $wp_query->post->post_parent;
} ?>
<?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
<div id="submenu">
<ul>
<?php wp_list_pages("title_li=&child_of=$parent" ); ?>
</ul>
</div>
<?php endif; ?>
U can try this code this code may be useful to u
<?php
global $wpdb, $post;
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = 'page' ORDER BY menu_order", 'OBJECT'); ?>
<?php if ( $child_pages ) :
foreach ( $child_pages as $pageChild ) :
setup_postdata( $pageChild ); ?>
<h2 class="subpagetitle">
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark"
title="<?php echo $pageChild->post_title; ?>">
<?php echo $pageChild->post_title; ?>
</a>
</h2>
<?php endforeach; endif; ?>

Wordpress post query php custom field conditional

Here's the situation:
In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.
All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.
Here's my WP_Query:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).
I've marked what I edited with comments (just one line).
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>

Resources